package com.yanchangapp.ui.activity.purchase;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.luck.picture.lib.decoration.RecycleViewDivider;
import com.luck.picture.lib.tools.ScreenUtils;
import com.yanchangapp.PtExtractAdapter;
import com.yanchangapp.R;
import com.yanchangapp.commom.LoadingType;
import com.yanchangapp.model.bean.ProductExtractBean;
import com.yanchangapp.ui.presenter.PtNmExtractAtPresenter;
import com.yanchangapp.ui.view.PtNmExtractAtView;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.util.ui.UIUtils;
import com.zxl.baselib.widget.YCLoadingView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 普通新建的合同提取....
 */
public class ProductNormalExtActivity extends BaseActivity<PtNmExtractAtView, PtNmExtractAtPresenter> implements PtNmExtractAtView {

    @BindView(R.id.ivToolbarNavigation)
    ImageView ivToolbarNavigation;
    @BindView(R.id.vToolbarDivision)
    View vToolbarDivision;
    @BindView(R.id.tvToolbarTitle)
    TextView tvToolbarTitle;
    @BindView(R.id.tv_publish_now)
    TextView tvPublishNow;
    @BindView(R.id.rvProductExtract)
    RecyclerView rvProductExtract;
    @BindView(R.id.tvDes)
    TextView tvDes;
    @BindView(R.id.llEmptyMsg)
    LinearLayout llEmptyMsg;
    @BindView(R.id.btnNextRefresh)
    TextView btnNextRefresh;
    @BindView(R.id.llErrorNetwork)
    LinearLayout llErrorNetwork;
    @BindView(R.id.ycLoading)
    YCLoadingView ycLoading;

    private PtExtractAdapter mPtExtractAdapter;
    private List<ProductExtractBean> mList;
    private String mSupplierID;

    private static final String SUPPLIER_ID = "supplier_id";
    private static final String DATA = "data";
    private static final int REQUEST_PRODUCT_CODE = 0x0098;
    private List<ProductExtractBean> mBeforeList = new ArrayList<>();

    @Override
    protected void init() {
        mList = new ArrayList<>();
        if (getIntent() != null){
            mSupplierID = getIntent().getStringExtra(SUPPLIER_ID);
            mBeforeList = (List<ProductExtractBean>) getIntent().getExtras().getSerializable(DATA);
        }else {
            finish();
        }
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_product_normal_ext;
    }

    public static void show(Activity activity,String supplierID,List<ProductExtractBean> list){
        Intent intent = new Intent(activity,ProductNormalExtActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString(SUPPLIER_ID, supplierID);
        bundle.putSerializable(DATA,(Serializable)list);
        intent.putExtras(bundle);
        activity.startActivityForResult(intent,REQUEST_PRODUCT_CODE);
    }

    @Override
    protected PtNmExtractAtPresenter createPresenter() {
        return new PtNmExtractAtPresenter(this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvToolbarTitle.setText("品种提取");
        tvPublishNow.setText("确定");
        tvPublishNow.setVisibility(View.VISIBLE);
        tvDes.setText("暂无品种");
        setLoadingState(LoadingType.LOADING);
        initRvAdapter();
    }

    private void initRvAdapter() {
        LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        rvProductExtract.setLayoutManager(manager);
        rvProductExtract.addItemDecoration(new RecycleViewDivider(
                this, LinearLayoutManager.HORIZONTAL, ScreenUtils.dip2px(this,0.5f), ContextCompat.getColor(this, R.color.borderColorBase)));
        mPtExtractAdapter = new PtExtractAdapter(R.layout.item_product_extract,mList);
        // adapter
        rvProductExtract.setAdapter(mPtExtractAdapter);
    }

    @Override
    protected void initData() {
        // 进行数据加载...
        mPresenter.querySupplierList(mSupplierID);
    }

    @Override
    protected void initListener() {
        mPtExtractAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                if (view.getId() == R.id.checkbox){
                    // 按钮的点击事件....
                    ProductExtractBean bean = mList.get(i);
                    // 取反...
                    bean.setChecked(!bean.isChecked());
                }
            }
        });

        ivToolbarNavigation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mPtExtractAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                // 按钮的点击事件....
                ProductExtractBean bean = mList.get(i);
                // 取反...
                bean.setChecked(!bean.isChecked());
                // 只是修改某个部门....
                mPtExtractAdapter.notifyItemChanged(i);
            }
        });

        btnNextRefresh.setOnClickListener(v -> {
            setLoadingState(LoadingType.LOADING);
            mPresenter.querySupplierList(mSupplierID);
        });

        tvPublishNow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 执行返回的逻辑....
                List<ProductExtractBean> mSelectLists = new ArrayList<>();
                if (mList.size() != 0){
                    for (int i=0;i<mList.size();i++){
                        ProductExtractBean bean = mList.get(i);
                        if (bean.isChecked()){
                            mSelectLists.add(bean);
                        }
                    }
                    if (mSelectLists.size() != 0){
                        Intent intent = new Intent();
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("data", (Serializable) mSelectLists);
                        intent.putExtras(bundle);
                        setResult(RESULT_OK,intent);
                        finish();
                    }else {
                        UIUtils.showToast("您还未选择品种");
                    }
                }
            }
        });
    }

    private void setLoadingState(LoadingType loadingType){
        switch (loadingType){
            case LOADING:
                if (ycLoading.getVisibility() == View.GONE){
                    ycLoading.setVisibility(View.VISIBLE);
                }

                if (llEmptyMsg.getVisibility() == View.VISIBLE){
                    llEmptyMsg.setVisibility(View.GONE);
                }

                if (llErrorNetwork.getVisibility() == View.VISIBLE){
                    llErrorNetwork.setVisibility(View.GONE);
                }
                break;
            case SUCCESS:
                if (ycLoading.getVisibility() == View.VISIBLE){
                    ycLoading.setVisibility(View.GONE);
                }

                if (llEmptyMsg.getVisibility() == View.VISIBLE){
                    llEmptyMsg.setVisibility(View.GONE);
                }

                if (llErrorNetwork.getVisibility() == View.VISIBLE){
                    llErrorNetwork.setVisibility(View.GONE);
                }
                break;
            case ERROR:
                if (ycLoading.getVisibility() == View.VISIBLE){
                    ycLoading.setVisibility(View.GONE);
                }

                if (llEmptyMsg.getVisibility() == View.VISIBLE){
                    llEmptyMsg.setVisibility(View.GONE);
                }

                if (llErrorNetwork.getVisibility() == View.GONE){
                    llErrorNetwork.setVisibility(View.VISIBLE);
                }
                break;
            case EMPTY:
                if (ycLoading.getVisibility() == View.VISIBLE){
                    ycLoading.setVisibility(View.GONE);
                }

                if (llEmptyMsg.getVisibility() == View.GONE){
                    llEmptyMsg.setVisibility(View.VISIBLE);
                }

                if (llErrorNetwork.getVisibility() == View.VISIBLE){
                    llErrorNetwork.setVisibility(View.GONE);
                }
                break;
            default:
                break;
        }

    }

    private void release(){
        if (ycLoading.getVisibility() == View.VISIBLE){
            ycLoading.setVisibility(View.GONE);
        }
    }

    @Override
    public void finish() {
        release();
        super.finish();
    }

    @Override
    public void queryPtExtractSuccess(List<ProductExtractBean> mLists) {
        if (mLists == null || mLists.size() == 0){
            setLoadingState(LoadingType.EMPTY);
        }else {
            setLoadingState(LoadingType.SUCCESS);
            this.mList.clear();
            this.mList.addAll(mLists);
            for (int i=0;i<mLists.size();i++){
                mLists.get(i).setSelectSize(1);
            }
            setSelectState();
            mPtExtractAdapter.notifyDataSetChanged();
        }

    }

    private void setSelectState() {
        if (mBeforeList.size() == 0){
            return;
        }
        // 查值并进行替换
        for (int i=0;i<mBeforeList.size();i++){
            ProductExtractBean beforeBean = mBeforeList.get(i);
            for (int j=0;j<mList.size();j++){
                ProductExtractBean bean = mList.get(j);
                if ((beforeBean.getProductId()).equals(bean.getProductId())){
                    mList.set(j,beforeBean);
                    break;
                }
            }
        }
    }

    @Override
    public void queryPtExtractFailure(String err) {
        UIUtils.showToast(err);
        setLoadingState(LoadingType.ERROR);
    }

}
